﻿using FlexFlow.Model;
using FlexFlow.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FlexFlow.MVC.Controllers.Ajax
{
    [Auth(MVC.AuthType.API)]
    public class FFEUserAPIController : Controller
    {
        /// <summary>
        /// 取得当前用户可以发起的流程列表
        /// </summary>
        /// <returns>
        /// 操作结果，其中tag为获取到的流程列表
        /// </returns>
        public ActionResult GetUserEnterableProcesses()
        {
            LoginedUserInfo uinfo = DefaultComponentSettings.OnlineUserPool.GetLoginedUserInfo(Request);
            return Json(WorkFlowEngine.Default.GetUserEnterableProcesses(uinfo.UserId));
        }
        /// <summary>
        /// 开始一个流程
        /// </summary>
        /// <param name="processId">要开始的流程ID</param>
        /// <param name="data">流程开始的附加数据</param>
        /// <returns>操作结果</returns>
        public ActionResult StartProcess(Guid processId, KeyValuePair<Guid, Object>[] data = null)
        {
            LoginedUserInfo uinfo = DefaultComponentSettings.OnlineUserPool.GetLoginedUserInfo(Request);
            return Json(WorkFlowEngine.Default.StartProcess(uinfo.UserId, processId, data));
        }
        /// <summary>
        /// 开始一个流程，并且指定流程起始表单
        /// </summary>
        /// <param name="processId">
        /// 流程Id
        /// </param>
        /// <param name="formId">
        /// 流程起始表单Id
        /// </param>
        /// <returns>
        /// 操作结果
        /// </returns>
        public ActionResult StartProcessWithForm(Guid procId, Guid formId)
        {
            Dictionary<Guid, Object> data = new Dictionary<Guid, object>();
            data[SystemDataSlots.ProcessEnterFormDataSlotID] = formId;
            LoginedUserInfo uinfo = DefaultComponentSettings.OnlineUserPool.GetLoginedUserInfo(Request);
            return Json(WorkFlowEngine.Default.StartProcess(uinfo.UserId, procId, data.ToArray()));
        }
        /// <summary>
        /// 提交审批任务
        /// </summary>
        /// <param name="taskId">
        /// 任务ID
        /// </param>
        /// <param name="accepted">
        /// 一个值，表示是否同意请求
        /// </param>
        /// <param name="message">
        /// 一个值，表式提交任务的附加消息
        /// </param>
        /// <returns>
        /// 操作结果
        /// </returns>
        public ActionResult SubmitApprovalTask(Guid taskId, bool accepted, string message = "")
        {
            LoginedUserInfo uinfo = DefaultComponentSettings.OnlineUserPool.GetLoginedUserInfo(Request);
            Dictionary<string, object> submitData = new Dictionary<string, object>();
            foreach (string key in Request.Form.AllKeys)
            {
                submitData[key] = Request.Form[key];
            }
            submitData["Accepted"] = accepted;
            OperateResult res = WorkFlowEngine.Default.SubmitTask(uinfo.UserId, taskId, submitData);
            return Json(res);
        }
        /// <summary>
        /// 取得当前用户绑定的流程实例列表
        /// </summary>
        /// <returns>
        /// 操作结果
        /// </returns>
        public ActionResult GetUserProcInstances()
        {
            LoginedUserInfo uinfo = DefaultComponentSettings.OnlineUserPool.GetLoginedUserInfo(Request);
            OperateResult res = WorkFlowEngine.Default.GetProcessInstancesByUID(uinfo.UserId);
            if (res.ResultCode == 200)
            {
                List<dynamic> resList = (List<dynamic>)res.Tag;
                return Json(new
                {
                    draw = 1,
                    recordsTotal = resList.Count,
                    recordsFiltered = resList.Count,
                    data = resList
                });
            }
            else
            {
                return Json(res.Tag);
            }
        }
        /// <summary>
        /// 取得当前用户要处理的任务列表
        /// </summary>
        /// <returns>
        /// 操作结果
        /// </returns>
        public ActionResult GetUserTasks()
        {
            LoginedUserInfo uinfo = DefaultComponentSettings.OnlineUserPool.GetLoginedUserInfo(Request);
            OperateResult res = WorkFlowEngine.Default.GetUserTasks(uinfo.UserId);
            if (res.ResultCode == 200)
            {
                List<dynamic> resList = (List<dynamic>)res.Tag;
                return Json(new
                {
                    draw = 1,
                    recordsTotal = resList.Count,
                    recordsFiltered = resList.Count,
                    data = resList
                });
            }
            else
            {
                return Json(res.Tag);
            }
        }

        /// <summary>
        /// 取得指定流程实例的步骤列表
        /// </summary>
        /// <param name="procInsId">
        /// 流程实例Id
        /// </param>
        /// <returns>
        /// 操作结果
        /// </returns>
        public ActionResult GetProcessSetps(Guid procInsId)
        {
            OperateResult res = WorkFlowEngine.Default.GetProcessSetps(procInsId);
            if (res.ResultCode != 200)
            {
                return Json(res);
            }
            else
            {
                List<FlowSetp> setps = res.Tag as List<FlowSetp>;
                List<dynamic> resList = new List<dynamic>();
                foreach (var setp in setps)
                {
                    resList.Add(new
                    {
                        id = setp.Identity,
                        fromActName = setp.FromActivityName,
                        fromUserName = setp.FromUserName,
                        toActName = setp.ToActivityName,
                        flowTime = setp.FlowTime.ToString(),
                        tag = setp.Tag
                    });
                }
                return Json(new
                {
                    draw = 1,
                    recordsTotal = resList.Count,
                    recordsFiltered = resList.Count,
                    data = resList
                });
            }
        }
    }
}
